package com.hopu.springbootdemo2.controller;

import com.hopu.springbootdemo2.util.OSSUtils;
import org.apache.tomcat.util.http.fileupload.impl.FileSizeLimitExceededException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;

@RestController
public class UploadController {

    @RequestMapping("/upload")
    protected ResponseEntity<String> upload(MultipartFile file) throws IOException {
        try {
            OSSUtils.upload(file.getInputStream(), file.getName());
            String url = OSSUtils.getURL(file.getName());
            return ResponseEntity.ok(url);
        }catch (FileSizeLimitExceededException ex){
            ex.printStackTrace();
            return ResponseEntity.ok("文件过大");
        }
    }

    @Value("${server.port}")
    private String port;

    @RequestMapping("/hello")
    public String hello(){
        return "Hello Nginx!!!" + port;
    }
}
